package cn.sinjet.model.carassist;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import cn.sinjet.prototol.codec.ToDevNaviCodec;

public class NaviInfoModule implements INaviInfo{
	final static String tag = NaviInfoModule.class.getSimpleName();
	Context mContext; 
	private int limitSpeed = 0;
    private int distance = 0, iconType = 0;
    private int bDspCamera = 0,cameraDistance = 0;
	private int retainDistance = 0;
	private boolean mIsBaiduNavigating = false;
    private boolean mIsAmapNavigating = false;
	private ToDevNaviCodec mNaviCodec = new ToDevNaviCodec();
   public NaviInfoModule(Context context){
	   mContext = context;
   }
   
  public void onNaviInfoMsg(Bundle data) {
	// TODO Auto-generated method stub
	int what = data.getInt("what");
	switch(what){
	case INaviInfo.STEP_INFO:{
		int iconType = data.getInt("iconType");
		int distance = data.getInt("distance");
		onStepInfo(iconType, distance);
	}break;
	case INaviInfo.LINE_INFO:{
		int retainDistance = data.getInt("retainDistance");
		onLineInfo(retainDistance);
	}break;
	case INaviInfo.LIMIT_SPEED:{
		int speed = data.getInt("speed");
		int limitSpeed = data.getInt("limitSpeed");
		onLimitSpeed(speed, limitSpeed);
	}break;
	case INaviInfo.CAMERA_DISTANCE:{
		int distance = data.getInt("distance");
		int dsp = data.getInt("dsp");
		int type = data.getInt("type");
		onCameraDistance(distance, dsp, type);
	}break;
	case INaviInfo.LANE_INFO:{
		int[] laneInfo = data.getIntArray("laneInfo");
		onLaneInfo(laneInfo);
	}break;
	case INaviInfo.BAIDU_NAVIGATING:{
		boolean isNavigating = data.getBoolean("isNavigating");
		onThirdPartyNavigating(isNavigating);
	}break;
	case INaviInfo.AMAP_NAVIGATING:{
		boolean isNavigating = data.getBoolean("isNavigating");
		onAmapNavigating(isNavigating);
	}break;
	}
  }

  
  private void clearData(){
	   limitSpeed = 0;
	   distance = 0; iconType = 0;
	   bDspCamera = 0;cameraDistance = 0;
	   retainDistance = 0;
  }
  
  @Override
	public void onStepInfo(int iconType, int distance) {
		// TODO Auto-generated method stub
//		isNaviRunning = 1;	
//		setNaviRunningForGettingInfo();
		Log.d(tag,"get front:"+distance+"m ::"+iconType);
		if(this.distance != distance || this.iconType != iconType){
			this.distance = distance;
	        this.iconType = iconType;
		    
			if(distance >= 2000 && iconType != 9)//not straight and distance is more than 150
				iconType = 9;
		
			mNaviCodec.sendnaviInfo(iconType, distance);
      }
	}


	@Override
	public void onLineInfo(int retainDistance) {
		// TODO Auto-generated method stub
		Log.d(tag,"get LineInfo retainDistance:"+retainDistance);
		if(this.retainDistance  == retainDistance)
			return;
//		isNaviRunning = 1;	
		this.retainDistance = retainDistance;
		mNaviCodec.sendLineInfo(retainDistance);
	}


	@Override
	public void onLimitSpeed(int speed, int limitSpeed) {
		// TODO Auto-generated method stub
//		isNaviRunning = 1;	
		if(this.limitSpeed == limitSpeed)
			return;
		this.limitSpeed = limitSpeed;
		mNaviCodec.sendLimitSpeed(speed,limitSpeed);
	}


	@Override
	public void onCameraDistance(int distance, int dsp,int type) {
		// TODO Auto-generated method stub
//		isNaviRunning = 1;	
		/*int bShouldDsp = 0;
		if(dsp == 0)  bShouldDsp = 0;
		else if(){	  
		   if(0<distance&&distance<=200)  bShouldDsp = 1;
		   else bShouldDsp = 0;
		}*/
		if(bDspCamera == dsp)
			return;
		this.cameraDistance = distance;
		this.bDspCamera = dsp;
		mNaviCodec.sendCameraDistance(cameraDistance,this.bDspCamera,type);
	}


	@Override
	public void onLaneInfo(int[] laneInfo) {
		// TODO Auto-generated method stub
//		isNaviRunning = 1;	
		mNaviCodec.sendLaneInfo(laneInfo);
	}

	@Override
	public void onThirdPartyNavigating(boolean isNavigating) {
		// TODO Auto-generated method stub
		if(mIsBaiduNavigating != isNavigating){
    		if(!isNavigating) clearData();
    		mNaviCodec.notifyIsNaviRunning((byte)isNavigating());
    	}
    	mIsBaiduNavigating = isNavigating;
	}

	private int isNavigating(){
    	Log.d(tag, "amap :"+mIsAmapNavigating+"  baidu:"+mIsBaiduNavigating);
    	if(mIsAmapNavigating||mIsBaiduNavigating)
    		return 1;
    	else 
    		return 0;
    }
	
	@Override
	public void onAmapNavigating(boolean isNavigating) {
		// TODO Auto-generated method stub
		if(mIsAmapNavigating != isNavigating){
    		if(!isNavigating) clearData();
    		mNaviCodec.notifyIsNaviRunning((byte)isNavigating());
    	}
    	mIsAmapNavigating = isNavigating;	
	}

	public void notifyNavigatingState() {
		// TODO Auto-generated method stub
		mNaviCodec.notifyIsNaviRunning((byte)isNavigating());
	}   
   
}
